//查询数据库
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

static int  callback(void *data,int columns,char**rows,char **columnNames){
  printf("%s\n",(char*)data);
  for(int i=0;i<columns;i++){
    //输出列名和对应的值
    printf("%d列名:%s=列值%s\n",i,columnNames[i],rows[i]);
  }
   printf("end\n"); 
  return 0;
}

int main(){
  //声明数据库指针
  sqlite3 *db;
  //错误信息
  char *errMsg;
  char *sql;
  //传递的数据
  char * data = "\n\n呼叫回调函数:\n";
  int res = sqlite3_open ("mydb.db", &db);
  if(res){
    fprintf (stderr, "打开数据库错误:%s\n",sqlite3_errmsg(db));
    exit (0);
  }
  //sql = "select * from student where name='%s' and " \
    //"password='%s'","aaa or 1=1 --", "12345 or 1=1 --";
 // sprintf (sql, "select * from student where name='%s' and password='%s'","aaa' or 1=1 --","123456' or 1=1 --");
 //防止sq注入
 // sql = sqlite3_mprintf ("select * from student where name='%q' and \
   // password='%q'","aaa' or 1=1 --", "12345' or 1=1 --");
   sql = "select * from user";
  printf("sql=%s\n", sql);
  res = sqlite3_exec (db, sql, callback, data, &errMsg);
  if(res!=SQLITE_OK){
    fprintf (stderr, "查询数据库的错误信息:%s\n",errMsg);
    sqlite3_free (errMsg);
  }else{
    printf("查询数据库...\n");
  }
  sqlite3_close (db);
  return 0;
}
